﻿Shader "Dan/UI/UI_Radar02"
{
    Properties
    {
        _Color ("Color", Color) = (0.08,0.4,0.8,1)
        _Emission ("Emission", Float ) = 1
        _LadarSpeed ("LadarSpeed", Float ) = 0.1
        _RadialGrid_X ("RadialGrid_X", Float ) = 32
        _RadialGrid_Y ("RadialGrid_Y", Float ) = 4
        _Grid_Width ("Grid_Width", Range(0, 1)) = 0.1
        _GridOpacity ("GridOpacity", Float ) = 0.5
        _WaveSpeed ("WaveSpeed", Float ) = 1
        _WaveOpacity ("WaveOpacity", Range(0, 1)) = 0
        [MaterialToggle] _UseTarget ("UseTarget", Float ) = 0
        _Direction ("Direction", Range(0, 1)) = 0
        _TargetPattern ("TargetPattern", 2D) = "white" {}
        _TargetSize ("TargetSize", Range(0, 1)) = 0.5
        _TargetSpeed ("TargetSpeed", Float ) = 0.5
        _Step ("Step", Float ) = 6
        _Distance ("Distance", Range(0,2)) = 1

    }
    
    SubShader
    {

        Tags { "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

            float4 _Color;
            float _Emission;
            float _RadialGrid_Y;
            float _RadialGrid_X;
            float _GridOpacity;
            float _WaveSpeed;
            float _LadarSpeed;
            float _Direction;
            sampler2D _TargetPattern; 
            float4 _TargetPattern_ST;
            float _TargetSpeed;
            float _Step;
            fixed _UseTarget;
            float _TargetSize;
            float _Grid_Width;
            float _WaveOpacity;
            float  _Distance;
            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;        
                return o;
            }
            
            float2 TargetMove (float2 uv , float direction, float size ,float speed ,float steps,float distance){
                float PI2 = 6.28318530718;    
                float rad1 = PI2*direction;
                float rad2 = (rad1+(PI2*(-1*(direction-0.125))));
                float2 center = float2(0.5,0.5);  
                float c1 = cos(rad1);
                float s1 = sin(rad1);
                float c2 = cos(rad2);
                float s2 = sin(rad2); 
                float detal = 4.0/max(size,0.1);     
                float pos = detal/2.0;  
                float2 dis = float2(pos,pos);
                float2x2 m = float2x2( c1, -s1, s1, c1);
                float2 rotateUV =uv*detal-dis;
                rotateUV = mul(rotateUV,m)+dis;
                float targetOffset = ((ceil((frac(speed)*steps))/steps)*(-distance*pos))+0.5;
                rotateUV+= float2(targetOffset,targetOffset)-center;
                float2 final = mul(rotateUV,float2x2(c2, -s2, s2, c2))+center;
                return final;     
            }


            fixed4 frag (v2f i ) : SV_Target
            {
                
                
         
                float2 uv  = i.uv;
                float PI2 = 6.28318530718;
                
                 
                float2 ruv = uv*2.0-1.0;
                float polar = atan2(ruv.x,ruv.y)/PI2+0.5;
                
                
                float scanLine = frac(polar -_Time.y*_LadarSpeed);
                
                scanLine *= scanLine;
                

                float circle = length(ruv);
                
                float targetspeed = _Time.y*_TargetSpeed;
                float2 rotUV =TargetMove(uv,_Direction,_TargetSize,targetspeed,_Step,_Distance);
                float2 targetUV = TRANSFORM_TEX(rotUV, _TargetPattern);
                float4 targetTex = tex2D(_TargetPattern,targetUV);
                float target = lerp( 0.0, targetTex.a*((1.0 - frac((targetspeed*_Step)))*2.5), _UseTarget );
                

               
                


                
                float ringU = step(frac(polar*_RadialGrid_X),_Grid_Width);
                float ringV = step(frac(circle*_RadialGrid_Y),_Grid_Width);
                float ring = max(ringU,ringV)*_GridOpacity;
                
                
                
                float waveSpeed = frac(_Time.y*_WaveSpeed);

                float radialLine = ((circle*2.0-1.0)-((waveSpeed-0.5)*4.0));
                

                float wave = saturate((radialLine*(1.0 - floor(radialLine))*(1.0 - waveSpeed)*_WaveOpacity));
                
                float radialMask = 1.0 - step(1,circle);
                
               
               
               float scanMask =saturate(1-circle)*scanLine;
               float lines = saturate(scanLine*3-2.75);
              
               float finalRadar = scanLine+ring*scanMask+wave+target+lines;
               
               float3 finalColor = _Color.rgb*_Emission;
              
               return fixed4(finalColor,finalRadar*radialMask);


            

            



            }
            ENDCG
        }
    }
    
    
    
}
